﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyLayouts;
using QLNH.QLNH_BUS_WS;
using System.Threading;

namespace QLNH.MyForms
{
    public partial class AdminMenuManagement : FormUC
    {
        #region Properties

        #endregion

        #region Constructor

        public AdminMenuManagement(LayoutUC mainLayout)
        {
            InitializeComponent();
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_MENU_MANAGEMENT;
        }

        #endregion

        #region Functions

        public override void Resize(Size size)
        {
            if (this.Size.Width < size.Width)
            {
                this.Location = new Point(size.Width / 2 - this.Size.Width / 2, size.Height / 2 - this.Size.Height / 2);
            }
            else
            {
                this.Location = new Point(0, 0);
            }
        }


        private string _nameToFind = "";
        //private string _catalogIDToSearch = "";
        private string _catalogToFind = "";

        //hien thi danh sach mon an
        private void ShowAllMeal()
        {
            this.lv_meals.Items.Clear();

            Thread t = new Thread(new ThreadStart(ShowAllMealThread));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();      
        }

        //hien thi tat ca cac mon an
        private List<MealDetailDTO> _mealList = null;
        private void ShowAllMealThread()
        {
            if (this._mealList == null)
            {
                _mealList = new List<MealDetailDTO>();

                QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
                MealDetailDTO[] temp = ws.GetAllMealDetail("", "");
                _mealList = temp.ToList<MealDetailDTO>();
            }

            List<MealDetailDTO> meal = _mealList.FindAll(e => e.Name.IndexOf(_nameToFind) >= 0 && e.Catalog.IndexOf(_catalogToFind) >= 0);

            for (int i = 0; i < meal.Count; i++)
            {
                ListViewItem li = new ListViewItem();
                li.Text = meal[i].ID;
                li.Name = meal[i].ID;
                li.SubItems.Add(meal[i].Name);
                li.SubItems.Add(meal[i].Unit);
                li.SubItems.Add(meal[i].Price.ToString());
                li.SubItems.Add(meal[i].Catalog);

                if (meal[i].Available == true)
                    li.SubItems.Add("Có sẵn");
                else
                    li.SubItems.Add("Không có sẵn");

                if (this.lv_meals.Items.Count % 2 != 0)
                {
                    li.BackColor = Color.LightGray;
                }

                this.lv_meals.BeginInvoke((MethodInvoker)(() => this.lv_meals.Items.Add(li)));
                Thread.Sleep(50);
            }
        }

        //xoa mon an
        private void RemoveMeal(string id)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();

            if (ws.RemoveMeal(id) == true)
            {
                MessageBox.Show("Xóa món ăn thành công");
                this._mealList.Remove(this._mealList.Find(e => e.ID.Equals(id)));
                ShowAllMeal();
            }
            else
            {
                MessageBox.Show("Xóa món ăn thất bại");
            }
        }

        #endregion

        #region Control Events

        private void bt_add_Click(object sender, EventArgs e)
        {
            this._mainLayout.SwitchForm(FormKey.ADMIN_MENU_EDIT);
        }

        private void bt_catalog_Click(object sender, EventArgs e)
        {
            this._mainLayout.SwitchForm(FormKey.ADMIN_CATALOG_MANAGEMENT);
        }

        private void bt_find_Click(object sender, EventArgs e)
        {
            _nameToFind = this.searchToolbar.StringToFind;
            _catalogToFind = this.searchToolbar.GroupNameToFind;

            ShowAllMeal();
        }

        private void bt_reload_Click(object sender, EventArgs e)
        {
            _nameToFind = "";
            _catalogToFind = "";
            ShowAllMeal();
        }

        CatalogDTO[] _catalogList = null;

        private void AdminMenuManagement_Load(object sender, EventArgs e)
        {
            ShowAllMeal();
            this.tooltip.Show("Nhắp chuột vào đây để sắp xếp lại", this.lv_meals, new Point(0, 0), 1000);
            if (_catalogList == null)
            {
                QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
                _catalogList = ws.GetAllCatalog();
                this.searchToolbar.SetGroupComboboxData(_catalogList, "Name", "ID");
            }
        }

        private void lv_meals_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && this.lv_meals.SelectedItems.Count > 0)
            {
                this.ctm_rightClick.Show(this.lv_meals, e.Location);
            }
        }

        private ListViewItemComparer sorter;
        private bool ascending = true;

        private void lv_meals_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            sorter = new ListViewItemComparer(e.Column);
            sorter.OrderOfSort = (ascending == true) ? SortOrder.Ascending : SortOrder.Descending;
            if (e.Column == 3)
                sorter.Numeric = true;
            this.lv_meals.ListViewItemSorter = sorter;
            this.lv_meals.Sort();
            ascending = !ascending;
        }

        private void ctm_action(object sender, EventArgs e)
        {
            int tag = Int32.Parse(((ToolStripMenuItem)sender).Tag.ToString());
            switch (tag)
            {
                case 0:
                    ListViewItem li = this.lv_meals.SelectedItems[0];
                    string id = li.Name;
                    string name = li.SubItems[1].Text;
                    string unit = li.SubItems[2].Text;
                    string price = li.SubItems[3].Text;
                    string catalog = li.SubItems[4].Text;
                    string available = li.SubItems[5].Text;

                    this._mainLayout.SwitchForm(FormKey.ADMIN_MENU_EDIT, new object[] { id, name, unit, price, catalog, available });
                    break;

                case 1:
                    if (MessageBox.Show("Bạn có thật sự muốn xóa món này?", "Nhắc nhở", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        string mealID = this.lv_meals.SelectedItems[0].Name;
                        this.RemoveMeal(mealID);
                    }
                    break;

                default:
                    break;
            }
        }

        #endregion        
    }
}
